package com.rxyb.redis.utils;

import com.rxyb.redis.constant.RedisConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * redis String类型工具类
 *
 * @author lcf
 * @date 2020/6/5
 */
@Slf4j
@Service
public class RedisStringUtils {


    @Autowired
    private StringRedisTemplate redisTemplate;


    /**
     * 设置redis和redis有效时间
     *
     * @param key
     * @param value
     * @param timeout  过期时间
     * @param timeUnit
     */
    public void set(String key, String value, TimeUnit timeUnit, long timeout) {
        redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
    }

    /**
     * 设置redi和redis有效时间
     *
     * @param group    拼接的前缀
     * @param key
     * @param value
     * @param timeUnit
     * @param timeout
     */
    public void set(String group, String key, String value, TimeUnit timeUnit, long timeout) {
        redisTemplate.opsForValue().set(RedisConstant.REDIS_STRING + "_" + group + "_" + key, value, timeout, timeUnit);
    }

    /**
     * 根据key获取redis的值
     *
     * @param key
     * @return
     */
    public Optional<String> get(String key) {
        String s = redisTemplate.opsForValue().get(key);
        if (null == s) {
            return Optional.empty();
        }
        return Optional.of(s);
    }

    /**
     * 根据key获取redis的值
     *
     * @param key
     * @return
     */
    public Optional<String> get(String group, String key) {
        String s = redisTemplate.opsForValue().get(RedisConstant.REDIS_STRING + "_" + group + "_" + key);
        if (null == s) {
            return Optional.empty();
        }
        return Optional.of(s);
    }

    /**
     * 根据key删除redis
     *
     * @param key
     */
    public void del(String key) {
        redisTemplate.delete(key);
    }

    /**
     * 根据key删除redis
     *
     * @param key
     */
    public void del(String group, String key) {
        redisTemplate.delete(RedisConstant.REDIS_STRING + "_" + group + "_" + key);
    }

    /**
     * 根据key删除redis
     *
     * @param group
     */
    public void delgroup(String group) {
        //最后一定要带上 *
        Set<String> keys = redisTemplate.keys(RedisConstant.REDIS_STRING + "_" + group + "_*");
        redisTemplate.delete(keys);
    }

    /**
     * 批量设置redis值
     *
     * @param map
     */
    public void bathSet(Map<String, String> map) {
        redisTemplate.opsForValue().multiSet(map);
    }

    /**
     * 获取以某些字符串为前缀的key列表
     * 例:mytest_* 组名+_+*
     *
     * @param prefix
     * @return
     */
    public Set<String> getKeys(String prefix) {
        Set<String> keys = redisTemplate.keys(prefix);
        return keys;
    }

    /**
     * 批量删除key
     *
     * @param keys 多个key
     */
    public void delAll(Collection<String> keys) {
        redisTemplate.delete(keys);
    }

    /**
     * 判断key是否存在
     *
     * @param key
     * @return
     */
    public boolean isExistKey(String key) {
        if (StringUtils.isBlank(key)) {
            return false;
        }
        return redisTemplate.hasKey(key);
    }

    /**
     * 修改key的名称
     *
     * @param oldKeyName
     * @param newKeyName
     */
    public void reKeyName(String oldKeyName, String newKeyName) {
        redisTemplate.rename(oldKeyName, newKeyName);
    }


}
